package com.li.youhappygo.aspect;

import com.li.youhappygo.annotation.Log;
import com.li.youhappygo.exception.GlobalException;
import com.li.youhappygo.model.entity.log.SysOperLog;
import com.li.youhappygo.model.vo.common.ResultCode;
import com.li.youhappygo.service.AsyncOperLogService;
import com.li.youhappygo.util.LogUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogAspect {


    @Autowired
    AsyncOperLogService asyncOperLogService;

    @Around(value = "@annotation(log)")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint,Log log){
        System.out.println("Log切面捕获");
        SysOperLog sysOperLog = new SysOperLog();
        LogUtil.doPreLog(log,proceedingJoinPoint,sysOperLog);
        Object proceed = null;

        try{
            proceed = proceedingJoinPoint.proceed();
            LogUtil.doAfterLog(log,proceedingJoinPoint,sysOperLog,0,null);
        } catch (Throwable e) {
            LogUtil.doAfterLog(log,proceedingJoinPoint,sysOperLog,1,e.getMessage());
            throw new GlobalException(ResultCode.DATA_ERROR);
        }
//        System.out.println(sysOperLog.getId()+"111111111111");

        asyncOperLogService.saveSysOperLog(sysOperLog);

        return proceed;
    }
}
